package com.mr.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mr.model.BaseInfo;
import com.mr.model.Member;
import com.mr.model.SearchCommand;
import com.mr.model.Status;
import com.mr.model.User;
import com.mr.util.DesUtils;
import com.mr.util.RandomUtil;
import com.mr.util.StringUtil;
import com.mr.util.ValidateUtil;
import com.mr.web.support.PageUtil;

@Controller
public class ShopController extends AnnotationController {
	@RequestMapping("/auth/member/shop/userForm.xhtml")
	public String userForm(Long id, ModelMap model){
		if(id!=null) {
			User user = daoService.getObject(User.class, id);
			model.put("user", user);
		}
		model.put("isMod", true);
		return "user/userForm.vm";
	}
	@RequestMapping("/auth/member/shop/userDetail.xhtml")
	public String userDetail(Long id, ModelMap model){
		if(id!=null) {
			User user = daoService.getObject(User.class, id);
			model.put("user", user);
		}
		return "user/userForm.vm";
	}
	@RequestMapping("/auth/member/shop/saveUser.xhtml")
	public String saveUser(Long id, HttpServletRequest req, ModelMap model) {
		BaseInfo base = (BaseInfo)req.getSession().getAttribute("base");
		User user = new User();
		if(id!=null) {
			user = daoService.getObject(User.class, id);
		}else {
			String membername = RandomUtil.getRandChar(6);
			String p = RandomUtil.getRandNum(8);
			String password = StringUtil.md5(p);
			String pwd = DesUtils.encrypt(p);
			Member member = new Member(membername, password, pwd, Member.MEMBERTYPE_USER, base);
			daoService.saveObject(member);
			id = member.getId();
		}
		try {
			bindData(user, req.getParameterMap());
			user.setId(id);
			user.setCsid(base.getId());
			String msg = ValidateUtil.validate(user, ",");
			if(StringUtils.isNotBlank(msg)) return showJsonFailure(msg, model);
			daoService.saveObject(user);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:/auth/member/shop/userList.xhtml";
	}
	@RequestMapping("/auth/member/shop/userList.xhtml")
	public String userList(SearchCommand command, Integer pageNo, HttpServletRequest req, ModelMap model){
		BaseInfo base = (BaseInfo)req.getSession().getAttribute("base");
		if(pageNo==null) pageNo = 0;
		Integer limit = 15;
		Integer count = commonService.getUserCount(base.getId(), command, Status.Y);
		List<User> userList = commonService.getUserList(base.getId(), command,  Status.Y, pageNo*limit, limit);
		
		Integer rowsPerPage = limit;
		PageUtil pageUtil = new PageUtil(count, rowsPerPage, pageNo, "auth/member/shop/userList.xhtml");
		Map params = new HashMap();
		params.put("csid", base.getId());
		pageUtil.initPageInfo(params);
		model.put("pageUtil", pageUtil);
		
		model.put("userList", userList);
		model.put("count", count);
		return "user/userList.vm";
	}
	@RequestMapping("/auth/member/shop/updateUser.xhtml")
	public String updateUser(String enId, ModelMap model){
		try {
			Long id = Long.valueOf(DesUtils.decrypt(enId));
			User user = daoService.getObject(User.class, id);
			user.setStatus(Status.D);
			daoService.saveObject(user);
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return showJsonFailure("修改数据错误！", model);
		}
		return showJsonSuccess(model);
	}
	@RequestMapping("/auth/member/shop/addPoint.xhtml")
	public String addPoint(Long userid, Integer point, String reason, HttpServletRequest req, ModelMap model) {
		BaseInfo base = (BaseInfo)req.getSession().getAttribute("base");
		commonService.addPoint(userid, base.getId(), point, reason);
		return showJsonSuccess(model);
	}
}
